package com.laughing.httpserver.util;

import com.laughing.httpserver.controller.DefaultController;
import com.laughing.httpserver.entity.MappingClassMethod;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author laughing.sheng <laughing1917@yeah.net/>
 * @date 2018/1/26
 * @see
 */
@Data
@Component
public class ControllerMappingHolder {

    private Map<String,MappingClassMethod> controllerMap=new HashMap<>();

    @PostConstruct
    public void  init() throws Exception{
        controllerMap.put("default",new MappingClassMethod(DefaultController.class,
                DefaultController.class.getMethod("notFound", HttpServletRequest.class, HttpServletResponse.class)));
        controllerMap.put("/",new MappingClassMethod(DefaultController.class,
                DefaultController.class.getMethod("process", HttpServletRequest.class, HttpServletResponse.class)));
        Collections.unmodifiableMap(controllerMap);
    }

    public MappingClassMethod getMappingClassMethod(String url){
        return controllerMap.get(url);
    }

    public Class getClass(String url){
        return controllerMap.get(url).getAClass();
    }

    public Method getMethod(String url){
        return controllerMap.get(url).getMethod();
    }
}
